package com.eqxiu.logserver.util;

import com.eqxiu.logserver.ipip.City;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.maxmind.geoip2.model.CityResponse;

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Constant {

    public static final int RESPONSE_CODE_NORMAL = 204;
    public static final int RESPONSE_CODE_INVALID_DOMAIN = 1000;
    public static final int RESPONSE_CODE_INVALID_EVENT_CONFIGE = 1001;
    public static final int RESPONSE_CODE_INVALID_PRODUCT = 1002;
    public static final int RESPONSE_CODE_PRODUCT_NAME_NOT_SET = 1003;
    public static final int RESPONSE_CODE_ERROR = 500;

    // 加载字段映射表
    public static Map<String, String> netTypeMap = new HashMap<>(); // n_t
    public static Map<String, String> mobileOperatorMap = new HashMap<>(); // m_o
    public static Map<String, String> manufacturerMap = new HashMap<>(); // man
    public static Map<String, String> operationSystemMap = new HashMap<>(); // os

    public static City city;
    public static String serverHost;

    public static void init(String ipFilePath){

        System.out.println(ipFilePath);
        try {
            serverHost = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            System.err.println("serverHost 解析出错：");
        }

        // 加载IP库
        String path = ipFilePath;
        try {
            city = new City(path);
        } catch (Exception e) {
            System.err.println("未找到IP库文件：" + path);
        }

        netTypeMap.put("WIFI", "WIFI");
        netTypeMap.put("wifi", "WIFI");
        netTypeMap.put("4G", "4G");
        netTypeMap.put("4g", "4G");
        netTypeMap.put("LTE", "4G");
        netTypeMap.put("LTE-CA", "4G");
        netTypeMap.put("3G", "3G");
        netTypeMap.put("3g", "3G");

        mobileOperatorMap.put("鹏博士/电信", "电信");
        mobileOperatorMap.put("电信", "电信");
        mobileOperatorMap.put("方正宽带/电信", "电信");
        mobileOperatorMap.put("科技网/电信", "电信");
        mobileOperatorMap.put("电信/联通", "电信");
        mobileOperatorMap.put("鹏博士/联通", "联通");
        mobileOperatorMap.put("联通", "联通");
        mobileOperatorMap.put("天威视讯/联通", "联通");
        mobileOperatorMap.put("浙江华数/联通", "联通");
        mobileOperatorMap.put("科技网/联通", "联通");
        mobileOperatorMap.put("联通/电信", "联通");
        mobileOperatorMap.put("铁通", "移动");
        mobileOperatorMap.put("移动", "移动");
        mobileOperatorMap.put("鹏博士/铁通", "移动");

        manufacturerMap.put("apple", "Apple");
        manufacturerMap.put("Apple", "Apple");
        manufacturerMap.put("iPhone7plus", "Apple");
        manufacturerMap.put("iPhone", "Apple");
        manufacturerMap.put("Huawei", "华为");
        manufacturerMap.put("HUAWEI", "华为");
        manufacturerMap.put("huawei", "华为");
        manufacturerMap.put("OPPO", "Oppo");
        manufacturerMap.put("Oppo", "Oppo");
        manufacturerMap.put("oppo", "Oppo");
        manufacturerMap.put("vivo", "Vivo");
        manufacturerMap.put("Vivo", "Vivo");
        manufacturerMap.put("Xiaomi", "小米");
        manufacturerMap.put("xiaomi", "小米");
        manufacturerMap.put("XiaoMi", "小米");
        manufacturerMap.put("blackshark", "小米");
        manufacturerMap.put("samsung", "Samsung");
        manufacturerMap.put("Samsung", "Samsung");
        manufacturerMap.put("SAMSUNG", "Samsung");
        manufacturerMap.put("Meizu", "魅族");
        manufacturerMap.put("meizu", "魅族");
        manufacturerMap.put("GIONEE", "金立");
        manufacturerMap.put("Gionee", "金立");
        manufacturerMap.put("GiONEE", "金立");
        manufacturerMap.put("Meitu", "美图");
        manufacturerMap.put("smartisan", "锤子");
        manufacturerMap.put("Smartisan", "锤子");
        manufacturerMap.put("360", "360");
        manufacturerMap.put("OnePlus", "一加");
        manufacturerMap.put("ONEPLUS", "一加");
        manufacturerMap.put("LeMobile", "乐视");
        manufacturerMap.put("lemobile", "乐视");
        manufacturerMap.put("Letv", "乐视");
        manufacturerMap.put("letv", "乐视");
        manufacturerMap.put("Nubia", "努比亚");
        manufacturerMap.put("nubia", "努比亚");
        manufacturerMap.put("ZTE", "中兴");
        manufacturerMap.put("Zte", "中兴");
        manufacturerMap.put("zte", "中兴");
        manufacturerMap.put("Coolpad", "酷派");
        manufacturerMap.put("coolpad", "酷派");
        manufacturerMap.put("HMD Global", "诺基亚");
        manufacturerMap.put("HMD Global Oy", "诺基亚");
        manufacturerMap.put("nokia", "诺基亚");
        manufacturerMap.put("Nokia", "诺基亚");
        manufacturerMap.put("LENOVO", "联想");
        manufacturerMap.put("ZUK", "联想");
        manufacturerMap.put("Lenovo", "联想");
        manufacturerMap.put("DOOV", "朵唯");
        manufacturerMap.put("Doov", "朵唯");
        manufacturerMap.put("hisense", "海信");
        manufacturerMap.put("Hisense", "海信");
        manufacturerMap.put("Sony", "索尼");
        manufacturerMap.put("sony", "索尼");
        manufacturerMap.put("Sony ericsson", "索尼");
        manufacturerMap.put("Gree", "格力");
        manufacturerMap.put("gree", "格力");
        manufacturerMap.put("GREE", "格力");
        manufacturerMap.put("asus", "华硕");
        manufacturerMap.put("ASUS", "华硕");
        manufacturerMap.put("Asus", "华硕");
        manufacturerMap.put("LGE", "LG");
        manufacturerMap.put("LG", "LG");
        manufacturerMap.put("Lg", "LG");
        manufacturerMap.put("BlackBerry", "黑莓");

        operationSystemMap.put("Android", "Android");
        operationSystemMap.put("iOS", "iOS");
        operationSystemMap.put("Windows", "PC");
        operationSystemMap.put("Mac OS X", "PC");
    }

    // 清理tracker冗余字段
    // u_a、lan、c_d、s_w、s_h、s_x、s_y、c_x、c_y、p_x、p_y、dom、pla、tit、c_t、sdk、ver、ip、ser、eng、o_v、i_c、lat、lon
    public static  Map<String, Object> clearTrackerCollection(Map<String, Object> collection) {
        collection.remove("u_a");
        collection.remove("lan");
        collection.remove("c_d");
        collection.remove("s_w");
        collection.remove("s_h");
        collection.remove("s_x");
        collection.remove("s_y");
        collection.remove("c_x");
        collection.remove("c_y");
        collection.remove("p_x");
        collection.remove("p_y");
        collection.remove("dom");
        collection.remove("pla");
        collection.remove("tit");
        collection.remove("sdk");
        collection.remove("ver");
//        collection.remove("ip");
        collection.remove("ser");
        collection.remove("srh");
        collection.remove("o_v");
        collection.remove("i_c");
        collection.remove("lat");
        collection.remove("lon");

        collection.remove("scene_id");
        collection.remove("scene_code");
        collection.remove("share_from");
        collection.remove("share_level");
        collection.remove("share_time");
        collection.remove("share_to");
        collection.remove("share_user");
        collection.remove("wx_cit");
        collection.remove("wx_cou");
        collection.remove("wx_pro");
        collection.remove("wx_hea");
        collection.remove("wx_n_n");
        collection.remove("wx_o_i");
        collection.remove("wx_u_i");
        collection.remove("wx_sex");

        collection.remove("version");
        collection.remove("channel");

        return collection;
    }

    // 清理tracker_view冗余字段
    // u_a、lan、c_d、s_w、s_h、s_x、s_y、c_x、c_y、p_x、p_y、dom、pla、c_t、sdk、ver、ip、ser、eng、o_v、i_c、lat、lon
    public static Map<String, Object> clearTrackerViewCollection(Map<String, Object> collection) {
        collection.remove("u_a");
        collection.remove("lan");
        collection.remove("c_d");
        collection.remove("s_w");
        collection.remove("s_h");
        collection.remove("s_x");
        collection.remove("s_y");
        collection.remove("c_x");
        collection.remove("c_y");
        collection.remove("p_x");
        collection.remove("p_y");
        collection.remove("dom");
        collection.remove("pla");
        collection.remove("sdk");
        collection.remove("ver");
//        collection.remove("ip");
        collection.remove("ser");
        collection.remove("srh");
        collection.remove("o_v");
        collection.remove("i_c");
        collection.remove("lat");
        collection.remove("lon");
        return collection;
    }

    // 解析服务器hostname
    public static String getServerHost() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "";
        }
    }

//    // 经纬度
//    public static Map<String, String> getGeoInfo(String ip) {
//        Map<String, String> map = new HashMap<>();
//        try {
//            CityResponse rep = GeoIp2Parser.getCityResponse(ip);
//            map.put("lat", rep.getLocation().getLatitude().toString()); // latitude
//            map.put("lon", rep.getLocation().getLongitude().toString()); // longitude
//        } catch (Exception e) {
//            System.err.println("位置信息解析失败！ ip: " + ip);
//        }
//        return map;
//    }

    // 国家、地区、城市、运营商
    public static  Map<? extends String, ?> getRegionInfo(String ip) {
        Map<String, String> map = new HashMap<>();
        try {
            String[] info = Constant.city.find(ip);
            if (info != null && info.length >= 3) {
                map.put("cou", info[0]); // country
                map.put("pro", info[1]); // province
                map.put("cit", info[2]); // city
            }
            String m_o = "";
            if (info != null && info.length >= 5) {
               m_o = info[4];
            }
            map.put("m_o", m_o ); // mobile operator
            map.put("m_o_2", Constant.mobileOperatorMap.getOrDefault(m_o, "Others"));

        } catch (Exception e) {
            System.err.println("区域信息解析失败！ ip: " + ip);
        }

        return map;
    }


//
//    // 根据LogConfig，生成JSONArray，格式：[{cat:分类, act:操作}, ...]
//    public static JsonArray getCategories(List<LogConfigManager.EventConfig> configs) {
//        JsonArray arr = new JsonArray();
//        for (LogConfigManager.EventConfig config : configs) {
//            JsonObject obj = new JsonObject();
//            obj.addProperty("cat", config.getEventCategory());
//            obj.addProperty("act", config.getEventAction());
//            arr.add(obj);
//        }
//        return arr;
//    }
//
//    // 根据UrlConfig，生成字符串，格式：name1,name2,...
//    public static List<String> getUrlNames(List<LogConfigManager.UrlConfig> configs) {
//        List<String> names = new ArrayList<>();
//        for (LogConfigManager.UrlConfig config : configs) {
//            names.add(config.getUrlName());
//        }
//        return names;
//    }

}
